package org.nf.box.web.controller;

import lombok.RequiredArgsConstructor;
import org.nf.box.common.result.ResultVO;
import org.nf.box.domain.Post;
import org.nf.box.service.PostService;
import org.nf.box.web.vo.PostVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author lzp
 * @Date 2024-12-10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/post")
public class PostController extends BaseController {

    private final PostService postService;

    /**
     * 根据状态查询所有帖子
     * @param status 帖子状态
     * @param userId 用户id
     * @return 帖子列表
     */
    @GetMapping("/all")
    public ResultVO<List<PostVO>> all(@RequestParam("status") String status,@RequestParam("userId") Integer userId) {
        List<PostVO> posts = postService.getPosts(status,userId);
        ResultVO<List<PostVO>> resultVO = new ResultVO<>();
        resultVO.setCode(200);
        resultVO.setData(posts);
        return resultVO;
    }

    /**
     * 获取视频帖子
     * @param status 帖子状态
     * @param userId 用户id
     * @return 帖子列表
     */
    @GetMapping("/video")
    public ResultVO<List<PostVO>> video(@RequestParam("status") String status,@RequestParam("userId") Integer userId) {
        List<PostVO> posts = postService.getVideo(status,userId);
        ResultVO<List<PostVO>> resultVO = new ResultVO<>();
        resultVO.setCode(200);
        resultVO.setData(posts);
        return resultVO;
    }

    /**
     * 获取推荐帖子
     * @param status 帖子状态
     * @param userId 用户id
     * @return 帖子列表
     */
    @GetMapping("/recommend")
    public ResultVO<List<PostVO>> recommend(@RequestParam("status") String status,@RequestParam("userId") Integer userId) {
        List<PostVO> posts = postService.getRecommend(status,userId);
        ResultVO<List<PostVO>> resultVO = new ResultVO<>();
        resultVO.setCode(200);
        resultVO.setData(posts);
        return resultVO;
    }

    /**
     * 根据帖子id查询帖子
     * @param postId 帖子id
     * @param userId 用户id
     * @return 帖子详情
     */
    @GetMapping("/detail")
    public ResultVO<PostVO> detail(@RequestParam("postId") int postId,@RequestParam("userId") Integer userId) {
        PostVO post = postService.postDetails(postId,userId);
        ResultVO<PostVO> resultVO = new ResultVO<>();
        resultVO.setCode(200);
        resultVO.setData(post);
        return resultVO;
    }

    /**
     * 发帖 + 文件上传
     * @return vo
     */
    @PostMapping("/add/{path}")
    public ResultVO post(//@RequestParam("postId") int postId,
                         @RequestParam("userId") int userId,
                         @RequestParam("title") String title,
                         @RequestParam("content") String content,
                         @RequestParam("image") String image,
                         @RequestParam("status") String status,
                         @RequestParam("uploadType") Integer uploadType,
                         @PathVariable("path") String prefix,
                         @RequestParam("files") MultipartFile[] files) {
        Post post = new Post();
        post.setUserId(userId);
        post.setTitle(title);
        post.setContent(content);
        post.setImage(image);
        post.setStatus(status);
        post.setUploadType(uploadType);
        postService.createPost(post,prefix,files);
        return new ResultVO<>(200,"发布成功",post);
    }

    /**
     * 编辑帖子 + 上传编辑后的文件
     * @return vo
     */
    @PostMapping("/update/{path}")
    public ResultVO update(@RequestParam("postId") int postId,
                         //@RequestParam("userId") int userId,
                         @RequestParam("title") String title,
                         @RequestParam("content") String content,
                         @RequestParam("image") String image,
                         @RequestParam("status") String status,
                         @PathVariable("path") String prefix,
                         @RequestParam("files") MultipartFile[] files) {
        Post post = new Post();
        post.setPostId(postId);
        //post.setUserId(userId);
        post.setTitle(title);
        post.setContent(content);
        post.setImage(image);
        post.setStatus(status);
        postService.updatePost(post,prefix,files);
        return new ResultVO<>(200,"编辑成功",post);
    }

    /**
     * 删帖
     * @param postId
     * @return
     */
    @GetMapping("/delete")
    public ResultVO deletePost(@RequestParam("postId") int postId) {
        postService.deletePost(postId);
        return new ResultVO<>(200,"删帖成功",null);
    }


}
